# -*- coding = utf-8 -*-
# @Time      : 2020/10/17 19:45
# @Author    : Reanon
# @File      : getData.py
# @Email     : dafo360@gmail.com
# @Software  : PyCharm

# 正则表达式，进行文字匹配
import re
# 指定URL，获取网页数据
import urllib.error
import urllib.request

# 网页解析，获取数据
from bs4 import BeautifulSoup

# 查找第一个单项
findItem = re.compile(r'<tr.*?>(.*?)</tr>', re.S)

# 创建正则表达对象，表示规则:开头是<a href=",中间任意长的字符，结尾是">
# (.*?):一个字符，零次到多次重复出现(.*)，出现0次到一次(?)
# 查找 Hardware Vendor
findHwVendor = re.compile(r'<td class="wkld_ssj_global_config_hw_vendor">(.*?)<.*?/td>', re.S)
findSystem = re.compile(r'<td class="wkld_ssj_global_config_hw_model">(.*?)<.*?/td>', re.S)
findHtml = re.compile(r'<a href="(.*?)>HTML</a>')
findText = re.compile(r'<a href="(.*?)>Text</a>')

findNodes = re.compile(r'<td class="hwnodes">(.*?)<.*?/td>')
findJvmVendor = re.compile(r'<td class="wkld_ssj_global_config_sw_jvm_vendor">(.*?)<.*?/td>')
findCpu = re.compile(r'<td class="wkld_ssj_global_config_hw_cpu">(.*?)<.*?/td>')
findMhz = re.compile(r'<td class="wkld_ssj_global_config_hw_cpu_mhz">(.*?)<.*?/td>')
findChip = re.compile(r'<td class="aggregate_config_cpu_chips">(.*?)<.*?/td>')
findCores = re.compile(r'<td class="aggregate_config_cpu_cores">(.*?)<.*?/td>')
findThreads = re.compile(r'<td class="hwtotalthreads">(.*?)<.*?/td>')
findMemory = re.compile(r'<td class="aggregate_config_memory_gb">(.*?)<.*?/td>')
findSsjOps = re.compile(r'<td class="submetric_ssjops_0">(.*?)<.*?/td>')
findPower1 = re.compile(r'<td class="submetric_power_0">(.*?)<.*?/td>')
findPower0 = re.compile(r'<td class="submetric_power_10">(.*?)<.*?/td>')
findRatio = re.compile(r'<td class="metric_performance_power_ratio">(.*?)<.*?/td>')


def askURL(url):
    """
    得到指定URL的网页内容
    :param url:
    :return:
    """

    # 模拟浏览器头部信息:向豆瓣服务器发送消息
    head = {
        # 用户代理；表示告诉豆瓣服务器，我们是什么类型的机器·浏览器（本质上是告诉浏览器·我们接收什么水平的文件
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36"
    }
    # 由于urlopen无法传参数，声明一个Request对象
    request = urllib.request.Request(url, headers=head)

    # 异常捕获
    try:
        response = urllib.request.urlopen(request)
        # 取得整个网页
        html = response.read().decode("utf-8")
    except urllib.error.URLError as e:
        if hasattr(e, "code"):
            print(e.code)
        if hasattr(e, "reason"):
            print(e.reason)
    return html


def getData(baseurl):
    """
    爬取网页，逐一解析数据

    :param baseurl:
    :return:
    """
    # 爬取网页
    # html = askURL(baseurl)

    # 读取本地网页
    html_local = open("./data/All Published SPEC SPECpower_ssj2008 Results.html", "rb")
    soup = BeautifulSoup(html_local, "html.parser")
    table = soup.find_all('tbody')
    table = str(table)

    # 统计有多少个单个数据
    flag = 0
    datalist = []
    for item in re.findall(findItem, table):
        # flag = flag + 1
        # 保存一条记录的全部信息
        data = []
        item = str(item)

        # 影片链接的详情
        hardwareVendor = re.findall(findHwVendor, item)
        system = re.findall(findSystem, item)
        html = re.findall(findHtml, item)
        text = re.findall(findText, item)
        nodes = re.findall(findNodes, item)
        jvmVender = re.findall(findJvmVendor, item)
        cpu = re.findall(findCpu, item)
        mhz = re.findall(findMhz, item)
        chips = re.findall(findChip, item)
        cores = re.findall(findCores, item)
        mem = re.findall(findMemory, item)
        ssj_ops = re.findall(findSsjOps, item)
        power = re.findall(findPower0, item)
        powerIdle = re.findall(findPower1, item)
        ration = re.findall(findRatio, item)

        data.append(hardwareVendor)
        data.append(system)
        data.append(html)
        data.append(text)
        data.append(nodes)
        data.append(jvmVender)
        data.append(cpu)
        data.append(mhz)
        data.append(chips)
        data.append(cores)
        data.append(mem)
        data.append(ssj_ops)
        data.append(power)
        data.append(powerIdle)
        data.append(ration)
        # print(flag)
        # 把数据放入
        datalist.append(data)
        # print(re.findall(findHwVendor, item))
    # print(datalist)
    return datalist
